package com.example.xinwenkehuduan;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.gdcp.Fragment.NewsFragment;
import cn.gdcp.FragmentAdapter.NewFragmentAdapter;

public class MainActivity extends AppCompatActivity {
    private ViewPager vpNewPager;
    private List<Fragment> fragmentList = new ArrayList<>();
    private NewFragmentAdapter newFragmentAdapter;
    private TextView tvRecommend;
    private TextView tvFinance;
    private TextView tvInternational;
    private TextView tvVideo;
    private TextView tvSport;
    private TextView tvRecreation;
    private TextView tvLike;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragment();
        initViewPager();

        tvLike = findViewById(R.id.tv_like);

        tvLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断用户是否登陆了  没登陆 就先去登陆

                if (BmobUser.getCurrentUser(BmobUser.class) ==null){
                    Intent intent = new Intent(MainActivity.this,LoginActivity.class);

                    startActivity(intent);
                    return;
                }

                Intent intent = new Intent(MainActivity.this,LikeActivity.class);

                startActivity(intent);

            }
        });
    }

    private void initFragment() {
        NewsFragment recommend = new NewsFragment();        //推荐
        recommend.setType("top");
        NewsFragment finance = new NewsFragment();          //财经
        finance.setType("caijing");
        NewsFragment international = new NewsFragment();    //国际
        international.setType("guoji");
        NewsFragment video = new NewsFragment();            //视频
        video.setType("junshi");
        NewsFragment sport = new NewsFragment();            //体育
        sport.setType("tiyu");
        NewsFragment recreation = new NewsFragment();       //娱乐
        recreation.setType("yule");


        fragmentList.add(recommend);
        fragmentList.add(finance);
        fragmentList.add(international);
        fragmentList.add(video);
        fragmentList.add(sport);
        fragmentList.add(recreation);

    }

    private void initViewPager() {
        tvRecommend = findViewById(R.id.tv_recommend);
        tvFinance   = findViewById(R.id.tv_finance);
        tvInternational = findViewById(R.id.tv_international);
        tvSport = findViewById(R.id.tv_sport);
        tvVideo = findViewById(R.id.tv_video);
        tvRecreation = findViewById(R.id.tv_recreation);



        tvRecommend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(0);
            }
        });

        tvFinance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(1);
            }
        });

        tvInternational.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(2);
            }
        });

        tvVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(3);
            }
        });

        tvSport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(4);
            }
        });

        tvRecreation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vpNewPager.setCurrentItem(5);
            }
        });

        tvRecommend.setBackgroundColor(Color.RED);
        vpNewPager = findViewById(R.id.vp_newPager);
        vpNewPager.setOffscreenPageLimit(6);
        newFragmentAdapter = new NewFragmentAdapter(getSupportFragmentManager(),fragmentList);
        vpNewPager.setAdapter(newFragmentAdapter);

        vpNewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position){
                    case 0:
                        tvRecommend.setBackgroundColor(Color.RED);
                        tvFinance.setBackgroundColor(Color.TRANSPARENT);
                        tvInternational.setBackgroundColor(Color.TRANSPARENT);
                        tvVideo.setBackgroundColor(Color.TRANSPARENT);
                        tvSport.setBackgroundColor(Color.TRANSPARENT);
                        tvRecreation.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 1:
                        tvRecommend.setBackgroundColor(Color.TRANSPARENT);
                        tvFinance.setBackgroundColor(Color.RED);
                        tvInternational.setBackgroundColor(Color.TRANSPARENT);
                        tvVideo.setBackgroundColor(Color.TRANSPARENT);
                        tvSport.setBackgroundColor(Color.TRANSPARENT);
                        tvRecreation.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 2:
                        tvRecommend.setBackgroundColor(Color.TRANSPARENT);
                        tvFinance.setBackgroundColor(Color.TRANSPARENT);
                        tvInternational.setBackgroundColor(Color.RED);
                        tvVideo.setBackgroundColor(Color.TRANSPARENT);
                        tvSport.setBackgroundColor(Color.TRANSPARENT);
                        tvRecreation.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 3:
                        tvRecommend.setBackgroundColor(Color.TRANSPARENT);
                        tvFinance.setBackgroundColor(Color.TRANSPARENT);
                        tvInternational.setBackgroundColor(Color.TRANSPARENT);
                        tvVideo.setBackgroundColor(Color.RED);
                        tvSport.setBackgroundColor(Color.TRANSPARENT);
                        tvRecreation.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 4:
                        tvRecommend.setBackgroundColor(Color.TRANSPARENT);
                        tvFinance.setBackgroundColor(Color.TRANSPARENT);
                        tvInternational.setBackgroundColor(Color.TRANSPARENT);
                        tvVideo.setBackgroundColor(Color.TRANSPARENT);
                        tvSport.setBackgroundColor(Color.RED);
                        tvRecreation.setBackgroundColor(Color.TRANSPARENT);
                        break;
                    case 5:
                        tvRecommend.setBackgroundColor(Color.TRANSPARENT);
                        tvFinance.setBackgroundColor(Color.TRANSPARENT);
                        tvInternational.setBackgroundColor(Color.TRANSPARENT);
                        tvVideo.setBackgroundColor(Color.TRANSPARENT);
                        tvSport.setBackgroundColor(Color.TRANSPARENT);
                        tvRecreation.setBackgroundColor(Color.RED);
                        break;
                }
            }
        });

    }
}
